<?php
namespace system\core;

use system\libraries\Translation;
use system\libraries\Session;

/**
 * Loader
 * 
 * The Loader class can load variables, views and helpers.
 * 
 * @author Steffen Brem <steffenbrem@gmail.com>
 * @package System.core
 */
class Loader 
{
    /**
     * Stores the data of the loaded variables
     * 
     * @var array
     */
    private static $_vars;

    /**
     * Load variables
     * 
     * Set all the variables so they are available in your view files.
     * 
     * @param array $vars 
     */
    public function vars( $vars )
    {
        if ( ! is_array($vars) )
        {
            throw new \Exception('Failed to assign variables, variables has to be an array.');
        }
        else
        {
            self::$_vars = $vars;
        }
    }
    
    /**
     * Load a view file
     * 
     * If you set return to TRUE, the view file will be buffered.
     * 
     * You can optionally set a view directory.
     * 
     * @param string $view
     * @param array $vars
     * @param bool $return
     * @param string $dir
     */
    public function view( $view, $vars = array(), $return = FALSE, $dir = 'views/' )
    {
        $view = strtolower($view);
        
        $request = Request::getInstance();
        
        $basePath = ($request->getModule()) ? APP_PATH . 'modules/' . $request->getModule() . '/' : APP_PATH;
        
        if ( ! file_exists($basePath . $dir . $view . '.phtml') ) 
        {
            throw new \Exception('View "' . $view . '.phtml" could not be loaded. View file does not exist.');
        }
        else
        {         
            if ( $vars && gettype($vars) != 'array' )
            {
                throw new \Exception('You did not set your data variables properly.');
            }
            
            $extract = array();
            
            if ( self::$_vars )
            {
                foreach ( self::$_vars as $index => $value )
                {
                    $extract[$index] = $value;
                }
            }
            
            if ( count($vars) > 0 )
            {
                foreach ( $vars as $index => $value )
                {
                    $extract[$index] = $value;
                }
            }

            extract($extract);
            
            ob_start();

            include $basePath . $dir . $view . '.phtml';
            
            $output = ob_get_contents();
            
            ob_end_clean();
            
            $parser = new Parser();
            $output = $parser->parse($output);

            if ( $return === TRUE )
            {
                return $output;
            }
            
            echo $output;
        }
    }
    
    /**
     * Load Helper
     * 
     * Load a helper by giving it's main name.
     * 
     * Example: $this->load->helper('url');
     * 
     * @access public
     * @param string $helper
     * @return void 
     */
    public function helper( $helper )
    {
        if ( file_exists(SYS_PATH . 'helpers/' . $helper . '_helper.php') )
        {
            require_once SYS_PATH . 'helpers/' . $helper . '_helper.php';
        }
        else
        {
            throw new \Exception('Helper "' . $helper . '" does not exist.');
        }
    }
}